/************************************************************
Copyright (c) 2012 Shenzhen State Micro Technology Co., Ltd.
All right reserved.

FileName:     port.h
Description:   

   Name       Version       Modification            Data         

*************************************************************/

#ifndef _PORT_H
#define _PORT_H

#ifndef	NULL
#define	NULL		(void *)0
#endif

extern int g_hPlay;
typedef unsigned long dvb_handle_t;
typedef   void (*task_entry_t)(void *pParam);

#define DVB_INVALID_HANDLE ((unsigned long)-1)

typedef  int (*pfDmxDataGotCb)( dvb_handle_t hFilter,  unsigned char* pu8Data,  unsigned short u16Datalen);
typedef  void (*pfDvbInitDoneCb)(int status);
typedef struct 
{
	unsigned short 	m_u16Pid;
	unsigned char		m_u8FilterDepth;
	unsigned char		m_u8CrcCheck;
	unsigned short*	m_pu16SectByte;
	pfDmxDataGotCb	m_pfCb;
	dvb_handle_t		m_hQueueID;
}filter_param_t;

typedef enum
{
	FLASH_USER_CONFIG,
	FLASH_USER_CONFIG_BK,
	FLASH_BOOKING,
	FLASH_CURSERVICE,
	FLASH_SIDATA,
}FLASH_ID;

typedef enum
{
	AUDIO_MODE_LEFT = 0,
	AUDIO_MODE_RIGHT,
	AUDIO_MODE_STEREO,
}STEREOMODE;

typedef enum
{
	DECODER_STATUS_NOT_PLAYING = 0,
	DECODER_STATUS_AV_PLAYING,
	DECODER_STATUS_VIDEO_PLAYING,
	DECODER_STATUS_AUDIO_PLAYING,
}DECODER_STATUS;

typedef enum
{
	DECODER_STATUS_SCRAMBLE_UNKNOWN = 0,
	DECODER_STATUS_AV_FREE,
	DECODER_STATUS_VIDEO_FREE,
	DECODER_STATUS_AUDIO_FREE,
	DECODER_STATUS_AV_SCRAMBLE,
}DECODER_SCRAMBLE_STATUS;

typedef struct tagTUNERINFO
{
	int				frequency;// 32   khz
	int				symbol_rate;// kS/s 
	int				modulation;//
	unsigned char		bandwidth;// M
	unsigned char		tunertype;//0 -t,1 -c
}dvb_tuner_t;

typedef struct
{
	int				frequency;//khz
	int				symbol_rate;//kS/s
	int				modulation;//
	unsigned char	bandwidth;//M
	unsigned char 	bLock; 
	unsigned char	level;
	unsigned char	snr; 
}dvb_tuner_status_t;

#define TS_FILE_NAME_MAX_BYTE        256
typedef enum
{
	DVB_DEMOD_DVB_DTMB,
	DVB_DEMOD_DVB_C,
}DVB_DEMOD_T;

typedef struct
{
	unsigned int		m_u32Freq;
	unsigned int		m_u32Mod;
	unsigned int		m_u32Sym;
	unsigned int		m_u32Bandwidth;
	unsigned int		m_u32Bitrate;
	DVB_DEMOD_T	m_eTunerType;
	char				m_as8Path[TS_FILE_NAME_MAX_BYTE];
}dvb_demo_param_t;


#define MW_DEBUG_COMMON		0
#define MW_DEBUG_ERROR		1
typedef void (*pfDebugOutput)(unsigned int u32Level, const char*);

#define	MAX_VOLUME_VALUE		100
extern void mwPlatformInit(pfDvbInitDoneCb callback,char *appFilesDir);
extern int mwPlatformTerm();
extern void mwDebug(char *format, ...);
extern void mwError(char *format, ...);
extern void mwRegistorDebugFunction(pfDebugOutput);
//os api
extern void mwOsSleep(int  ticks);//ms
extern dvb_handle_t mwOsCreateMessage(unsigned int MaxMsgNum, unsigned int MaxMsgRecLen);
extern int mwOsDeleteMessage(dvb_handle_t msgqhandle);
extern int mwOsSendMessage(dvb_handle_t msgqhandle,void * msg,int len,int timeout);
extern int mwOsReceiveMessage(dvb_handle_t msgqhandle,void *msg,int len,int timeout);
extern int  mwOsEmptyMessage(dvb_handle_t msgqhandle);
extern dvb_handle_t  mwOsCreateSemaphore(unsigned int value);
extern int mwOsDeleteSemaphore (dvb_handle_t semaphore);
extern int mwOsWaitSemaphore(dvb_handle_t semaphore,unsigned int timeout);
extern int mwOsSignalSemaphore (dvb_handle_t semaphore);
extern unsigned int mwOsGetTickCount(void);
extern int mwOsCreateTask(unsigned char *name, int prio, int stacksize, unsigned long *id, int mode, task_entry_t func, void *param );
extern int mwOsTaskDestroy(dvb_handle_t taskid);
extern dvb_handle_t mwOsMutexCreate(char *name);
extern  int mwOsMutexLock(dvb_handle_t hMutex);
extern  int mwOsMutexUnlock(dvb_handle_t hMutex);
extern int mwOsMutexDestroy(dvb_handle_t hMutex);
extern void *mwOsAllocMemory(int size);
extern void mwOsFreeMemory( void *p);
//tuner api
extern int mwTunerReset(int ReOpenflag);
extern int mwTunerLock(dvb_tuner_t *status, unsigned int u32Force);
extern int mwTunerRelock();
extern int mwTunerGetStatus(dvb_tuner_status_t *status);
extern int mwTunerCurFreq(dvb_tuner_t * pTUNERINFO);
//demux api
extern int mwDmxStartFilter(filter_param_t* pParam, dvb_handle_t *hfilter);
extern int mwDmxStopFilter(dvb_handle_t hfilter);
extern int mwDmxEnableFilter(dvb_handle_t handle);
extern int mwDmxDisableFilter(dvb_handle_t handle);
//nvm api
extern int  mwNvmRead(FLASH_ID readId, unsigned int Offset, unsigned char *targetData,  int numBytes);
extern int  mwNvmWrite(FLASH_ID writeId, unsigned int Offset, unsigned char *sourceData,  int numBytes);
//play api
extern int mwAvPlay(int hplay, unsigned short pmtPID, unsigned short serID, unsigned short pcrPID, unsigned short vidPID, unsigned short vidType, unsigned short audPID, unsigned short audType);
extern int mwAvStop(int hplay);
extern int mwAvBlackScreen();
extern int mwAvSetStereoMode (STEREOMODE mode);
extern int mwAvSetVolume(int vol);
extern int mwAvSwitchAudio(unsigned short audPID, unsigned short audType);
extern int mwAvGetPlayStatus(DECODER_STATUS* peStatus);
extern int mwCIStartDescrable(unsigned char* pu8Pmt, unsigned short u16Len);

extern void* mwDLOpen(char* ps8Path);
extern void* mwDLLoadSymbol(void* phFile, char* ps8FuncName);
extern void mwDLClose(void* phFile);
#endif//_PORT_H
